package com.joplus.housekeeper.network;


import android.content.Context;

import com.alibaba.fastjson.JSONObject;

import com.hyphenate.chatuidemo.DemoApplication;

import com.joplus.housekeeper.bean.*;
import com.joplus.housekeeper.core.Config;

import com.joplus.housekeeper.utils.L;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by sc on 2015/10/30.
 */
public class RequestHelper {

    private static RequestHelper mInstance;


    private RequestHelper() {
    }

    public static RequestHelper getInstance() {

        if (mInstance == null) {
            synchronized (RetrofitHelper.class) {
                if (mInstance == null) {
                    mInstance = new RequestHelper();
                }
            }
        }

        return mInstance;
    }


    public void orderStatus(Context context, String orderId, TextCallback listener) {
        String url = Config.SERVICE_PATH + "order/status/" + orderId;
        RetrofitHelper.getInstance().get(context, url, null, listener);
    }

    public void downLoad(String url, File file, TextCallback listener) {

        RetrofitHelper.getInstance().downLoad(url, file, listener);
    }

    public void getorderList(Context context, String accountSid, TextCallback listener) {
        String url = Config.SERVICE_PATH + "orderInfo/listRecently10?accountSid=" + accountSid;
        RetrofitHelper.getInstance().getFast(context, url, OrderOutBeanNew.class, listener);
    }


    /**
     * 获取用户资料
     *
     * @param context
     * @param listener
     */
    public void getInfo(Context context, TextCallback listener) {
        String url = Config.SERVICE_PATH + "account/getInfo";
        RetrofitHelper.getInstance().get(context, url, User.class, listener);
    }


    /**
     * 是否满意
     *
     * @param evaluate_type 1是满意  2是不满意
     * @param reward_id     客服id
     */
    public void setSatisfaction(String evaluate_type, String reward_id, String evalute,
                                TextCallback listener) {


        Map<String, Object> params = new HashMap();
        params.put("evaluateType", evaluate_type);
        params.put("rewardId", reward_id);
        params.put("evaluate", evalute);
        String url = Config.SERVICE_PATH + "rewardEvaluate/updateReward";
        RetrofitHelper.getInstance().post(url, params, null, listener);
    }


    public void CheckMessages(Context context, String date, String accountSid, TextCallback listener) {
        String url = Config.SERVICE_PATH + "imRecord/list?date=" + date + "&mobile=" + accountSid;
        RetrofitHelper.getInstance().get(context, url, ChatPastBean.class, listener);
    }

    public void chatTime(Context context, String accountId, TextCallback listener) {
        String url = Config.SERVICE_PATH + "imRecord/listDates?accountId=" + accountId;
        RetrofitHelper.getInstance().get(context, url, ChatMessageBean.class, listener);
    }

    public void allService(Context context, TextCallback listener) {
        String url = Config.SERVICE_PATH + "ofuser/listCutomerServices";
        RetrofitHelper.getInstance().get(context, url, ServiceList.class, listener);

    }

    public void imLogin(String userName, String password, TextCallback listener) {
        String url = Config.SERVICE_PATH + "ofuser/login";
        Map<String, Object> map = new HashMap<>();
        map.put("userName", userName);
        map.put("password", password);
        map.put("deviceToken", JPushInterface.getRegistrationID(DemoApplication.getInstance()));
        RetrofitHelper.getInstance().post(url, map, CodeBean.class, listener);
    }

    public void upPic(File file, String messageId, TextCallback listener) {

        Map<String, RequestBody> params = new HashMap();

        params.put("messageId", convertToRequestBody(messageId));

        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part avatarFile = MultipartBody.Part.createFormData("imgFile", file.getName(), requestBody);
        String url = Config.SERVICE_PATH + "picture/upload";
        RetrofitHelper.getInstance().postFile(url, params, avatarFile, null, listener);
    }


    public void imRecod(Context context, int startRow, int pageSize, TextCallback listener) {
        String url = Config.SERVICE_PATH + "imRecord/listDialogues?pageSize=" + pageSize + "&startRow=" + startRow;
        RetrofitHelper.getInstance().get(context, url, MainBean.class, listener);

    }

    public void recordList(Context context, String accountId, int startRow, int pageSize, TextCallback listener) {
        String url = Config.SERVICE_PATH + "imRecord/list?accountId=" + accountId + "&startRow=" + startRow + "&pageSize=" + pageSize;
        RetrofitHelper.getInstance().get(context, url, RecordListBean.class, listener);
    }

    private RequestBody convertToRequestBody(String param) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), param);
        return requestBody;
    }


    public void getImRecord4Date(Context context, long messageDatetime, String accountId, TextCallback textCallback) {
        String url = Config.SERVICE_PATH + "imRecord/searchRecentMessage?accountId=" + accountId + "&messageDatetime=" + messageDatetime;
        RetrofitHelper.getInstance().get(context, url, null, textCallback);
    }

    public void searchAccount(Context context, int startRow, String s, TextCallback listener) {
        String url = Config.SERVICE_PATH + "imRecord/searchAccount?startRow=" + startRow + "&pageSize=20&keyword=" + s;
        RetrofitHelper.getInstance().get(context, url, ImSearchAccountBean.class, listener);
    }

    public void searchMessage(Context context, int startRow, String s, TextCallback listener) {
        String url = Config.SERVICE_PATH + "imRecord/searchMessage?startRow=" + startRow + "&pageSize=20&keyword=" + s;
        RetrofitHelper.getInstance().get(context, url, ImSearchMessageBean.class, listener);
    }

    public void getNote(Context context,String accountId, TextCallback listener) {
        String url = Config.SERVICE_PATH + "customerNote/getDetail?accountId="+accountId;
        RetrofitHelper.getInstance().get(context, url, CustomerNoteBean.class, listener);
    }

    public void saveNote(String messageCustomerNoteParam, TextCallback listener) {
        String url = Config.SERVICE_PATH + "customerNote/save";
        Map<String, Object> map = new HashMap<>();
        map.put("messageCustomerNoteParam", messageCustomerNoteParam);
        RetrofitHelper.getInstance().post(url, map, CodeBean.class, listener);
    }


}

